<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">大码新增</p>
    </div>
    <div class="view-container">
      <el-row class="status-title">
        <el-col class="mr20 width100">
            <el-button
                class="btn-success width100"
                type="primary"
                @click="submitForm()"
            >提交</el-button>
        </el-col>
      </el-row>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px" class="mt30">
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产计划:" prop="planCode">
              <el-input v-model="form.planCode" placeholder="请选择生产计划" @focus="planShow" clearable @clear="planClear" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品名称:" prop="productBasicName">
              <el-input v-model="form.productBasicName" placeholder="请输入产品名称" disabled />
            </el-form-item>
          </el-col>
        </el-row><el-row>
          <el-col :span="12">
            <el-form-item label="规格名称:" prop="productName">
              <el-input v-model="form.productName" placeholder="请输入规格名称" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="批次号:" prop="batchNumber">
              <el-input v-model="form.batchNumber" placeholder="请输入批次号" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
             <el-form-item label="大码规格:" prop="specs">
              <el-input v-model.number="form.specs" placeholder="请输入大码规格" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="备注:" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 弹出数据 -->
    <el-dialog title="生产计划" :visible.sync="open" width="70%" top="50px" append-to-body center :before-close="handleClose">
      <el-form :model="queryParams" ref="queryForm" :inline="true" @submit.native.prevent>
        <el-form-item label="产品名称" prop="productName">
          <el-input
            v-model="queryParams.productName"
            placeholder="请输入名称或编码检索"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="dataList" @row-click="onSelectData" :default-sort="{prop: 'date', order: 'descending'}" border>
        <el-table-column label="序号" type="index" align="center" width="50">
           <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column label="计划编码" align="center" prop="planCode" />
        <el-table-column label="产品名称" align="center" prop="productBasicName" />
        <el-table-column label="规格名称" align="center" prop="productName" />
        <el-table-column label="产品批号" align="center" prop="batchNumber" />
        <el-table-column label="码格式" align="center" prop="codeFormat">
          <template slot-scope="scope">
            <div v-if="scope.row.codeFormat == 1">一物一码</div>
            <div v-if="scope.row.codeFormat == 2">UDI码</div>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="productStatus">
          <template slot-scope="scope">
            <div v-if="scope.row.plunStatus == 0" style="color:#52C41A">计划中</div>
            <div v-if="scope.row.plunStatus == 1" style="color:#52C41A">进行中</div>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" align="center" prop="approveStatus">
          <template slot-scope="scope">
            <div v-if="scope.row.approveStatus == 1" style="color:#52C41A">已审核</div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getProduct"
      />
    </el-dialog>
  </div>
</template>

<script>
import { palletCodeAdd, udiPlanList } from "@/api/system/palletCode/palletCode"
import { listProduct } from  "@/api/system/quickcode/udicode"
// import { udiPlanList } from "@/api/system/plan/plan"
export default {
  name: "add",
  data() {
    return {
      form: {
        planId: '', // 生产计划id
        planCode: '', // 生产计划编码
        productBasicName: '', // 产品名称
        productName: '', // 规格名称
        specs: '', // 大码规格
        batchNumber: '', // 批次号
        remark: '', // 备注
      },
      // 表单校验
      rules: {
        planCode: [
        { required: true, message: "生产计划不能为空", trigger: "blur" }
        ],
      },
      open: false,
      loading: true,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: undefined,
      },
      // 弹出数据列表
      dataList: [],
    }
  },
  methods: {
    // 生产计划弹窗显示
    planShow(){
      this.open = true
      this.getProduct()
    },
    // 请求生产计划列表
    getProduct(){
      this.loading = true
      udiPlanList(this.addDateRange(this.queryParams)).then(res => {
        this.dataList = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    // 删除产品
    planClear(){
      this.form.planId = ''
      this.form.planCode = ''
      this.form.productBasicName = ''
      this.form.productName = ''
      this.form.specs = ''
      this.form.batchNumber = ''
      this.form.remark = ''
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getProduct()
    },
    // 重置按钮
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 选中产品
    onSelectData(row){
      // console.log(row)
      this.form.planId = row.id
      this.form.planCode = row.planCode
      this.form.productBasicName = row.productBasicName
      this.form.productName = row.productName
      this.form.batchNumber = row.batchNumber
      listProduct({ productId: row.productId }).then(res => {
        this.form.specs = res.data.largeSizeSpecifications
        this.open = false
        this.queryParams.productName = ''
      })
    },
    // 关闭弹窗
    handleClose(){
      this.open = false
      this.queryParams.productName = undefined
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$modal.loading("正在提交")
          palletCodeAdd(this.form).then(res => {
            if(res.code == 200){
              this.$modal.closeLoading()
              this.$refs.form.resetFields()
              this.$tab.closeOpenPage({ path: "/palletCode/list" })
              this.$modal.msgSuccess("新增成功!")
            }
          })
        }
      })
    }
  }
};
</script>
